#include "StdAfx.h"
#include "AMobileDeviceListener.h"
#include "AMobileDeviceModule.h"

AMobileDevice::AMobileDevice()
{
}

AMobileDevice::~AMobileDevice()
{
}

AMobileDeviceListenerImpl::AMobileDeviceListenerImpl(void) :
	lpAMDeviceNotification(NULL)
{
}

AMobileDeviceListenerImpl::~AMobileDeviceListenerImpl(void)
{
}

void AMobileDeviceListenerImpl::Register(AMobileDevice* lpDevice)
{
	lpDeviceList.push_back(lpDevice);
}

void AMobileDeviceListenerImpl::Listen()
{
	AMobileDeviceModuleImpl& module = AMobileDeviceModule::GetInstance();
	module.Load();
	(*module.lpf_AMDeviceNotificationSubscribe)(&AMobileDeviceListenerImpl::AMDeviceNotificationCallback, 0, 0, 0, &lpAMDeviceNotification);
}

void AMobileDeviceListenerImpl::Close()
{
	AMobileDeviceModuleImpl& module = AMobileDeviceModule::GetInstance();
	(*module.lpf_AMDeviceNotificationUnsubscribe)(lpAMDeviceNotification);
	module.Unload();
}

void AMobileDeviceListenerImpl::AMDeviceNotificationCallback(am_device_notification_callback_info * callback)
{
	AMobileDeviceListenerImpl listener = AMobileDeviceListener::GetInstance();
	if(ADNCI_MSG_CONNECTED == callback->msg)
	{
		for(vector<AMobileDevice*>::iterator iter = listener.lpDeviceList.begin();
			iter != listener.lpDeviceList.end();
			++iter )
			(*iter)->OnConnected(callback->dev);
	}
	else if(ADNCI_MSG_DISCONNECTED == callback->msg)
	{
		for(vector<AMobileDevice*>::iterator iter = listener.lpDeviceList.begin();
			iter != listener.lpDeviceList.end();
			++iter )
			(*iter)->OnDisconnected(callback->dev);
	}
}

